package com.ydlclass.listeners;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;

import java.util.Random;

public class MessageListener implements ApplicationListener<OrderEvent> {

	Logger log = LoggerFactory.getLogger(MessageListener.class);


	@Override
	public void onApplicationEvent(OrderEvent event) {
		Object source = event.getSource();
		if(source instanceof OrderDetail orderDetail) {
			// 模拟发送短信
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
			log.info("{} -> 短信----》订单详情：{}",Thread.currentThread().getId(),orderDetail);

			if(new Random().nextInt(100) % 2 == 0)
				throw new RuntimeException("短信发送失败");

		}
	}
}
